package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.DishService;
import com.sky.service.ShoppingCartService;
import com.sky.utils.CurrentHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /*
     *
     * 添加商品到购物车
     * */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {


        // 查询当前菜品或套餐是否在购物车中
        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(CurrentHolder.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO, cart);

        List<ShoppingCart> list = shoppingCartMapper.list(cart);//因为查询条件比较足所以只会查到一条,或者没查到

        // 如果已经存在，数量加1
        if (list != null && list.size() > 0) {
            ShoppingCart cartInDB = list.get(0);
            cartInDB.setNumber(cartInDB.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cartInDB);
            return;
        }
        // 如果不存在，添加到购物车，数量默认为1
        //判断本次添加的是菜品还是套餐
        Long dishId = shoppingCartDTO.getDishId();
        if (dishId != null) {
            //本次是菜品
            Dish dish = dishMapper.getById(dishId);
            cart.setName(dish.getName());
            cart.setImage(dish.getImage());
            cart.setAmount(dish.getPrice());

            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
        } else {
            //本次是套餐

            Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
            cart.setName(setmeal.getName());
            cart.setImage(setmeal.getImage());
            cart.setAmount(setmeal.getPrice());
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
        }
        shoppingCartMapper.insert(cart);

    }

    /*
     * 查询购物车
     * */
    public List<ShoppingCart> showShoppingCart() {

        Long userid = CurrentHolder.getCurrentId();
        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(userid);
        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        return list;
    }

    @Override
    public void clean() {
        Long userid = CurrentHolder.getCurrentId();
        shoppingCartMapper.deleteByUserId(userid);
    }

    /*
     * 删除购物车中一个商品(数量减一,如果number==1则直接删掉)
     * */
    public void sub(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(CurrentHolder.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO, cart);

        //从数据库查询该菜品或套餐在购物车中的数据
        List<ShoppingCart> list = shoppingCartMapper.list(cart);//只能查出一条且不可能为空
        ShoppingCart shoppingCart = list.get(0);
        // 查询当前菜品或套餐在购物车中的数量
        Integer number = shoppingCart.getNumber();
        // 如果数量为1，则直接删除
        if (number == 1) {
            shoppingCartMapper.deleteById(shoppingCart.getId());
        } else {
            // 如果数量不为1，则修改数量
            shoppingCart.setNumber(number - 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }
    }
}
